import java.util.Scanner;

public class UserService {
    private Scanner scanner;

    public UserService(Scanner scanner) {
        this.scanner = scanner;
    }

    // 用户登录
    public User login() {
        System.out.print("请输入用户名: ");
        String username = scanner.nextLine();
        System.out.print("请输入密码: ");
        String password = scanner.nextLine();

        User user = CinemaData.findUserByUsername(username);
        if (user != null && user.getPassword().equals(password)) {
            System.out.println("登录成功！欢迎回来，" + username + "！");
            return user;
        } else {
            System.out.println("登录失败：用户名或密码错误！");
            return null;
        }
    }

    // 修改密码
    public boolean changePassword(User currentUser, String oldPassword, String newPassword) {
        if (!currentUser.getPassword().equals(oldPassword)) {
            System.out.println("原密码错误，修改失败！");
            return false;
        }

        currentUser.setPassword(newPassword);
        System.out.println("密码修改成功！");
        return true;
    }

    // 管理员：重置用户密码
    public boolean resetPassword(String username, String newPassword) {
        User user = CinemaData.findUserByUsername(username);
        if (user == null) {
            System.out.println("用户不存在！");
            return false;
        }

        user.setPassword(newPassword);
        System.out.println("用户 " + username + " 的密码已重置！");
        return true;
    }

    // 管理员：添加用户
    public boolean addUser() {
        System.out.print("请输入新用户名: ");
        String username = scanner.nextLine();

        // 检查用户名是否已存在
        if (CinemaData.findUserByUsername(username) != null) {
            System.out.println("用户名已存在！");
            return false;
        }

        System.out.print("请输入密码: ");
        String password = scanner.nextLine();
        System.out.print("请输入角色 (admin/manager/front): ");
        String role = scanner.nextLine();

        // 验证角色合法性
        if (!"admin".equals(role) && !"manager".equals(role) && !"front".equals(role)) {
            System.out.println("角色输入错误！");
            return false;
        }

        User newUser = new User(username, password, role);
        if (CinemaData.addUser(newUser)) {
            System.out.println("用户添加成功！");
            return true;
        } else {
            System.out.println("用户添加失败，系统用户已达上限！");
            return false;
        }
    }

    // 管理员：删除用户
    public boolean deleteUser(String username) {
        // 不能删除自己
        if ("admin".equals(username)) {
            System.out.println("不能删除管理员账户！");
            return false;
        }

        if (CinemaData.deleteUser(username)) {
            System.out.println("用户 " + username + " 已删除！");
            return true;
        } else {
            System.out.println("删除失败，用户不存在！");
            return false;
        }
    }

    // 管理员：列出所有用户
    public void listAllUsers() {
        User[] users = CinemaData.getAllUsers();
        System.out.println("\n=== 系统用户列表 ===");
        System.out.println("用户名\t\t角色");
        System.out.println("-----------------------");
        for (User user : users) {
            System.out.println(user.getUsername() + "\t\t" + user.getRole());
        }
    }
}

